package tw.twandroidproj.app.ui.Apply;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tw.twandroidproj.app.R;
import tw.twandroidproj.app.model.ErrorModel;
import tw.twandroidproj.app.network.NetworkManager;
import tw.twandroidproj.app.network.OnResponseListener;
import tw.twandroidproj.app.ui.adapter.ReLayoutManager;

public class JobApply extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemSelectedListener {

    TextView selectJobTime_Add;
    Button jobApply;
    RelativeLayout selectResume;
    resumePopupWindow resumeWindow;
    View timeSelectDiaV;
    Spinner spinner_dayS, spinner_dayE, spinner_hourS, spinner_hourE;
    ArrayAdapter<Integer> dayArrayAdapter;
    ArrayAdapter<String> hourArrayAdapter;
    List<Integer> dayList;
    List<String> hourList;
    int selected_dayS, selected_dayE;
    String selected_hourS, selected_hourE, selected_jobTime;
    int year, month, dayS = 15, dayE = 26, hourS = 8, hourE = 17;
    Dialog timeselectDialog;
    RecyclerView selectJobTime_List;
    jobApply_timeAdapter jobapply_timeAdapter;
    Map<String,String> map =new HashMap<String,String>();
    int position;
    int resume_id;
    private NetworkManager networkManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_jobtime);
        networkManager=NetworkManager.getInstance(this);
        Bundle bundle = getIntent().getExtras();
        setSupportActionBar((android.support.v7.widget.Toolbar) findViewById(R.id.toolbar));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        position = (int) bundle.get("Job_id");

        Toast.makeText(this, String.valueOf(position), Toast.LENGTH_LONG).show();
        selectJobTime_List = (RecyclerView) findViewById(R.id.selectJobTime_List);
        jobapply_timeAdapter = new jobApply_timeAdapter(this);
        ReLayoutManager recycleManager = new ReLayoutManager(this);
        recycleManager.setOrientation(OrientationHelper.VERTICAL);
        selectJobTime_List.setLayoutManager(recycleManager);
        selectJobTime_List.setAdapter(jobapply_timeAdapter);

        selectJobTime_Add = (TextView) findViewById(R.id.selectJobTime_Add);
        selectJobTime_Add.setOnClickListener(this);

        selectResume = (RelativeLayout) findViewById(R.id.selResume);
        selectResume.setOnClickListener(this);

        jobApply = (Button) findViewById(R.id.applyJob);
        jobApply.setOnClickListener(this);

        LayoutInflater inflater = LayoutInflater.from(JobApply.this);
        timeSelectDiaV = inflater.inflate(R.layout.timeselect_dialog, null);
        spinner_dayS = (Spinner) timeSelectDiaV.findViewById(R.id.dayS);
        spinner_dayE = (Spinner) timeSelectDiaV.findViewById(R.id.dayE);
        spinner_hourS = (Spinner) timeSelectDiaV.findViewById(R.id.hourS);
        spinner_hourE = (Spinner) timeSelectDiaV.findViewById(R.id.hourE);

        initList();     //设置数据源
        dayArrayAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, dayList);
        hourArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, hourList);
        dayArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        hourArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_dayS.setAdapter(dayArrayAdapter);
        spinner_dayE.setAdapter(dayArrayAdapter);
        spinner_hourS.setAdapter(hourArrayAdapter);
        spinner_hourE.setAdapter(hourArrayAdapter);
        spinner_dayS.setOnItemSelectedListener(this);
        spinner_dayE.setOnItemSelectedListener(this);
        spinner_hourS.setOnItemSelectedListener(this);
        spinner_hourE.setOnItemSelectedListener(this);

        timeselectDialog = new AlertDialog.Builder(this).
                setView(timeSelectDiaV).
                setTitle("选择空余时间").
                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        selected_jobTime = "5" + "/" + selected_dayS + "至" +
                                "5" + "/" + selected_dayE + "(" + selected_hourS + "至" + selected_hourE + ")";
//                                添加获得的选择时间数据并刷新listview
                        Log.i("selected_jobTime", selected_jobTime);
                        jobapply_timeAdapter.jobApply_Time.add(selected_jobTime);
                        jobapply_timeAdapter.notifyDataSetChanged();
                    }
                }).
                setNegativeButton("取消", null).
                create();
    }

    private void initList() {
        dayList = new ArrayList<Integer>();
        hourList = new ArrayList<String>();
        for (int i = dayS; i <= dayE; i++) {
            dayList.add(i);
        }
        for (int i = hourS; i <= hourE; i++) {
            hourList.add(i + ":00");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case (R.id.selResume):
                resumeWindow = new resumePopupWindow(this);
                resume_id=1;
                if (resumeWindow.resumeData.isEmpty())

                    Toast.makeText(this, "你还没有简历！", Toast.LENGTH_SHORT).show();
                else
                    resumeWindow.showAtLocation(findViewById(R.id.jobApply_main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                break;

            case (R.id.selectJobTime_Add):
                //  timeselectDialog.show();


                break;
            case (R.id.applyJob):

                new AlertDialog.Builder(this)
                        .setTitle("确认")
                        .setMessage("确定申请该工作吗？")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                map.put("job_id",String.valueOf(position));
                                map.put("resume_id",String.valueOf(resume_id));
                                networkManager.postJobApply(map, new OnResponseListener<tw.twandroidproj.app.model.JobApply>() {
                                 @Override
                                 public void onSuccess(tw.twandroidproj.app.model.JobApply result) {
                                     Toast.makeText(getBaseContext(), "你还没有简历！", Toast.LENGTH_SHORT).show();
                                     Intent intent =new Intent();
                                     intent.setClass(JobApply.this,apply_state.class);
                                     startActivity(intent);

                                 }
                                 @Override
                                 public void onFailed(ErrorModel errorModel) {

                                     Log.e("TAG", "F: ");
                                 }
                             });

                            }
                        })
                        .setNegativeButton("否", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .show();


                break;


            default:
                break;
        }
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.dayS:
                selected_dayS = dayList.get(position);
                break;
            case R.id.dayE:
                selected_dayE = dayList.get(position);
                break;
            case R.id.hourS:
                selected_hourS = hourList.get(position);
                break;
            case R.id.hourE:
                selected_hourE = hourList.get(position);
                break;
            default:
                break;
        }
    }


    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:

                return super.onOptionsItemSelected(item);
        }
    }
}